-- KEYS [good]
-- ARGV [uid]
-- return -1-库存不足 0-重复购买 1-成功

local good = KEYS[1]
local activity = ARGV[1]
local uid = ARGV[2]
local gooduids = good .. ':' .. activity .. ':uids'

local isin = redis.call('SISMEMBER', gooduids, uid)

if isin > 0 then
  return 0
end

local goodstock = good .. ':' .. activity .. ':stock'
local stock = redis.call('GET', goodstock)

if not stock or tonumber(stock) <= 0 then
  return -1
end

redis.call('DECR', goodstock)
redis.call('SADD', gooduids, uid)
return 1